﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

///<summary>
///
///</summary>
public class GameObjectPool
{
    List<GameObject> GameObjList = new List<GameObject>();
    Dictionary<string, List<GameObject>> ObjDict = new Dictionary<string,List<GameObject>>();
    static GameObjectPool instance;
    private GameObjectPool() { }

    List<GameObject> list = new List<GameObject>();
    public static GameObjectPool GetInstance()
    {
        if (instance == null)
        {
            instance = new GameObjectPool();
        }
        return instance;
    }
    //生成物体并返回
    public GameObject GetObject(string path, string key)
    {
        GameObject obj = GetObjFromDict(key);
        if (obj == null)
        {
            obj = GameObject.Instantiate(Resources.Load<GameObject>(path));
            GameObjList.Add(obj);
            if (!ObjDict.ContainsKey(key))
            {
                ObjDict.Add(key, GameObjList);
            }
        }
        obj.SetActive(true);
        return obj;
    } 

    GameObject GetObjFromDict(string key)
    {
        if (!ObjDict.ContainsKey(key))
        {
            return null;
        }
        Debug.Log(ObjDict.Count);
        foreach (GameObject obj in ObjDict[key])
        {
            if (!obj.activeInHierarchy&&obj.name==key)
            {
                return obj;
            }
        }
        return null;
    }
}
